package com.dingding.service.user.core.dao.impl;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.UserTaxiOrdert;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.order.dto.grab.PushTaxiGrabOrder;
import com.dingding.service.user.core.dao.ITaxiGrabOrderDao;
import com.dingding.service.user.core.sql.TaxiGrabOrderSQL;

/**
 * 
 * @ClassName: TaxiGrabOrderDaoImpl 
 * @Description: 出租车抢单DAO实现类
 * @author YHQ
 * @date 2016年6月30日 下午4:53:41
 * 
 */
@Component("taxiGrabOrderDao")
public class TaxiGrabOrderDaoImpl extends BaseDaoImpl<UserTaxiOrdert> implements ITaxiGrabOrderDao {

	private Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * 
	 * @Title: getTaxiNoticeContent
	 * @Description: 出租车抢单通知接口
	 * @param placeOrderUserId 下单人Id
	 * @param grabOrderUserId  抢单人Id
	 * @author yuHaiQing
	 * @return PushCompetitiveOrder    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public PushTaxiGrabOrder getTaxiNoticeContent(String placeOrderUserId, String grabOrderUserId) {
		
		//创建
		Object[] userDetalObj = null;
		
		try {
			userDetalObj = super.sqlQueryUnResultObj(TaxiGrabOrderSQL.PASSAGER_GRABORDER_GET_USER_INFO, placeOrderUserId, grabOrderUserId);
		} catch (Exception e) {
			log.error("获取用户抢单通知内容时出现异常   类：" + this.getClass() + " getNoticeContent()", e);
		}
		
		//出租车抢单，通知内容(发送短信，推送消息)
		PushTaxiGrabOrder pushTaxiGrabOrder = null;
		
		if (userDetalObj != null) {
			
			pushTaxiGrabOrder =  new PushTaxiGrabOrder();
			
			// 数据转换
			converBean(userDetalObj, pushTaxiGrabOrder);
		}
		
		return pushTaxiGrabOrder;
	}
	
	/**
	 * 
	 * @Title: converBean 
	 * @Description:将查询到的数据转换成用户抢单推送bean 
	 * @param userDetalObj 数据库中查询到的数据 
	 * @param pushCompetitiveOrder 推送数据封装bean 
	 */
	private void converBean(Object[] userDetalObj, PushTaxiGrabOrder pushTaxiGrabOrder) {
		
		// 下单用户别名
		if (userDetalObj[0] != null) {
			pushTaxiGrabOrder.setAliasId(userDetalObj[0].toString());
		}
		// 下单用户手机号
		if (userDetalObj[1] != null) {
			pushTaxiGrabOrder.setAnotherPhoneNo(userDetalObj[1].toString());
		}
		// 下单用户设备类型
		if (userDetalObj[2] != null) {
			
			// 获取设备类型
			String equipmentType = userDetalObj[2].toString();

			if (equipmentType.equals("1")) { // 如果设备类型等于1，表示该设备类型为iOS
				pushTaxiGrabOrder.setLoginEquipment("iOS");
			} else if (equipmentType.equals("2")) { // 如果设备类型等于2，表示该设备类型为Android
				pushTaxiGrabOrder.setLoginEquipment("Android");
			}
			
		}
		// 司机昵称
		if (userDetalObj[3] != null) {
			pushTaxiGrabOrder.setNickName(userDetalObj[3].toString());
		}
		// 司机性别
		if (userDetalObj[4] != null) {
			pushTaxiGrabOrder.setSex(ToolUtil.castInteger(userDetalObj[4]));
		}
		// 司机手机号
		if (userDetalObj[5] != null) {
			pushTaxiGrabOrder.setTellPhone(userDetalObj[5].toString());
		}
		// 司机头像
		if (userDetalObj[6] != null) {
			pushTaxiGrabOrder.setUserAvatar(ToolUtil.imageAddress(userDetalObj[6].toString()));
		}
		// 司机头像缩略图
		if (userDetalObj[7] != null) {
			pushTaxiGrabOrder.setUserTabloidAvatar(ToolUtil.imageAddress(userDetalObj[7].toString()));
		}
		// 车辆品牌
		if (userDetalObj[8] != null) {
			pushTaxiGrabOrder.setVehicleType(userDetalObj[8].toString());
		}
		// 车辆型号
		if (userDetalObj[9] != null) {
			pushTaxiGrabOrder.setVehicleModel(userDetalObj[9].toString());
		}
		// 车辆颜色
		if (userDetalObj[10] != null) {
			pushTaxiGrabOrder.setVehicleColor(userDetalObj[10].toString());
		}
		// 车辆照片
		if (userDetalObj[11] != null) {
			pushTaxiGrabOrder.setVehicleImage(ToolUtil.imageAddress(userDetalObj[11].toString()));
		}
		// 车牌号
		if (userDetalObj[12] != null) {
			pushTaxiGrabOrder.setLicensePlate(userDetalObj[12].toString());
		}
		// 车主信誉度
		if (userDetalObj[13] != null) {
			pushTaxiGrabOrder.setUserLevel(userDetalObj[13].toString());
		}

	}

	@Override
	@Transactional(readOnly = true)
	public PushTaxiGrabOrder getTaxiDriverNoticeContent(String placeOrderUserId, String grabOrderUserId) {
		// 创建
		Object[] driverDetalObj = null;

		try {
			driverDetalObj = super.sqlQueryUnResultObj(TaxiGrabOrderSQL.DRIVER_GRABORDER_GET_USER_INFO,placeOrderUserId,grabOrderUserId);
		} catch (Exception e) {
			log.error("获取用户抢单通知内容时出现异常   类：" + this.getClass() + " getNoticeContent()", e);
		}

		// 出租车抢单，通知内容(发送短信，推送消息)
		PushTaxiGrabOrder pushTaxiGrabOrder = null;

		if (driverDetalObj != null) {

			pushTaxiGrabOrder = new PushTaxiGrabOrder();

			// 数据转换
			converBean(driverDetalObj, pushTaxiGrabOrder);
		}

		return pushTaxiGrabOrder;
	}
	
}
